package com.wsc.wisdomchinaqinghe.adapter;

import android.content.Context;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 基础adapter类。可以提供用数组绑定listView的功能
 */
public abstract class ArrayListAdapter<T> extends BaseAdapter {

	protected List<T> mList;
	protected Context mContext;

	public ArrayListAdapter(Context context) {
		mContext = context;
	}

	public ArrayListAdapter(Context context, List<T> list) {
		mContext = context;
		mList = new ArrayList<T>();
		if (list != null) {
			mList.addAll(list);
		}
	}

	public List<T> getList() {
		return mList;
	}

	public void setList(List<T> list) {
		mList = list;
		notifyDataSetChanged();
	}

	public void setList(T[] array) {
		List<T> list = new ArrayList<T>(array.length);
		for (T t : array) {
			list.add(t);
		}
		setList(list);
	}

	@Override
	public int getCount() {
		if (mList != null)
			return mList.size();
		else
			return 0;
	}

	@Override
	public Object getItem(int position) {
		return mList == null ? null : mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

}
